<?php
namespace app\appletadmin\controller;
use app\admin\controller\Common;
use think\Db;
use think\Log;
class Goods extends Common
{
	public function showlist()
	{
		$data=Db::table("applet_goods")->select();
		$this->assign("data",$data);
		return $this->fetch();
	}
	public function add()
	{
		if(request()->isPost())
		{
			//获取表单数据
            $data = request()->post();
            $file = request()->file('pic');
            if(!empty($file)){
                
                $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'uploads'. DS .'icon');
                if($info){
                	$name=str_replace('\\', '/',$info->getSaveName());
                    $data['icon'] = "http://www.jfzhiwen.com/uploads/icon/".$name; 
                }else{
                    echo $file->getError();
                    die;
                }  
            }else{
                $this->error('请选择要上传的图片');
            }
                  
            //插入数据到数据库
            $res=Db::table("applet_goods")->insert($data);
            if($res){
                 $this->success('添加成功',url('showlist'));
            }else{
                 $this->error('添加失败');
            }
		}else
		{
			return $this->fetch();
		}
	}
	public function edit()
	{
		if(request()->isPost())
		{
			//获取表单数据
            $data = request()->post();
            //将数据插入数据库
            $result = Db::table("applet_goods")->update($data);;
            //判断数据是否插入成功
            if($result)
            {
                $this->success('修改成功',url('showlist'));
            }else
            {
                $this->error('修改失败',url('showlist'));
            }

		}else
		{
			$id=request()->get("id");
			$info=Db::table("applet_goods")->where("id",$id)->find();
			$this->assign("info",$info);
			return $this->fetch();
		}
	}

	public function del()
	{
		$id=request()->get("id");
        
        $res=Db::table("applet_goods")->where("id",$id)->delete();
        if($res){
            $url=url('showlist');
            echo "<script>alert('删除成功');location.href='$url'</script>";
            exit;
            
        }else{
            $this->error("删除失败");
        }   
	}
}